/**
 * 文件名：LogisticsVehicleController.java
 *
 * 创建人：Fyh - 1009173473@qq.com
 *
 * 创建时间：2019年9月25日 下午5:19:45
 *
 * 版权所有：南京植百汇智能科技服务有限公司
 */
package com.z100h.supplychain.evidence.web;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.rribbit.RequestResponseBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.jeeplus.common.utils.RRBHints;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.z100h.supplychain.evidence.entity.LogisticsVehicle;
import com.z100h.supplychain.evidence.entity.LogisticsVehicleListVo;
import com.z100h.supplychain.evidence.entity.Worker;
import com.z100h.supplychain.evidence.facade.LogisticsVehicleFacade;

/**
 * [描述信息：说明类的基本功能]
 *
 * @author Fyh - 1009173473@qq.com
 * @version 1.0 Created on 2019年9月25日 下午5:19:45
 */
@Controller
@RequestMapping(value = "${adminPath}/sc/logisticsVehicle")
public class LogisticsVehicleController extends BaseController {

	@Autowired
	private LogisticsVehicleFacade logisticsVehicleFacade;

	@Autowired
	private RequestResponseBus rrb;

	@RequestMapping(value = "list")
	public String list() {
		return "supplychain/logistics/list";
	}

	@RequestMapping(value = "assignList")
	public String assignList() {
		return "supplychain/logistics/assignList";
	}

	@RequestMapping(value = "detail")
	public String detail(HttpServletRequest request, String customerId, String serviceType) {
		request.setAttribute("customerId", customerId);
		request.setAttribute("serviceType", serviceType);
		return "supplychain/logistics/detail";
	}

	@RequestMapping(value = "loadList")
	public String loadList() {
		return "supplychain/logistics/loadList";
	}

	@ResponseBody
	@RequestMapping(value = "findByPage")
	public Map<String, Object> findByPage(Page<LogisticsVehicle> page, LogisticsVehicle logisticsVehicle,
			HttpServletRequest request, HttpServletResponse response) {
		page = logisticsVehicleFacade.findByPage(page, logisticsVehicle);
		return getBootstrapData(page);
	}

	/**
	 * 
	 * 待分配的生产列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月26日 上午10:23:00
	 */
	@RequestMapping(value = "findByLogisticsVehicleList")
	@ResponseBody
	public Map<String, Object> findByLogisticsVehicleList(HttpServletRequest request, HttpServletResponse response) {
		Page<LogisticsVehicleListVo> page = new Page<LogisticsVehicleListVo>(request, response);
		List<LogisticsVehicleListVo> list = logisticsVehicleFacade.findByLogisticsVehicleList();
		page.setList(list);
		return getBootstrapData(page);
	}

	@ResponseBody
	@RequestMapping(value = "save")
	public String save(LogisticsVehicle logisticsVehicle, String workerId, String arr) {
		try {
			// JSONArray ary = JSONArray.parseArray(StringEscapeUtils.unescapeHtml4(arr));
			List<LogisticsVehicleListVo> LogisticsVehicleList = JSONArray
					.parseArray(StringEscapeUtils.unescapeHtml4(arr), LogisticsVehicleListVo.class);
			logisticsVehicleFacade.save(logisticsVehicle, LogisticsVehicleList, workerId);
			// 记录物流派车流程
			rrb.send(RRBHints.PRODUCTION_PROC.LOGISTICS_PRODUCE, logisticsVehicle.getId(), null);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}

	@ResponseBody
	@RequestMapping(value = "findLoadByPage")
	public Map<String, Object> findLoadByPage(Page<LogisticsVehicle> page, LogisticsVehicle logisticsVehicle,
			HttpServletRequest request, HttpServletResponse response) {
		page = logisticsVehicleFacade.findLoadByPage(page, logisticsVehicle);
		return getBootstrapData(page);
	}

	/**
	 * 
	 * 装车确认
	 * 
	 * @param ids
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年10月21日 下午3:50:28
	 */
	@ResponseBody
	@RequestMapping(value = "finish")
	public String finish(String ids) {
		try {
			String[] arr = ids.split(",");
			List<String> list = Arrays.asList(arr);
			logisticsVehicleFacade.finish(list);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}

	@ResponseBody
	@RequestMapping(value = "findSjType")
	public List<Worker> findSjType() {
		return logisticsVehicleFacade.findSjType();
	}
}
